#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define LENGTH sizeof(struct ContactPerson)
struct ContactPerson
{
    char Name[20];
	char Occupation[20];
	char Title[20];
	char Phonenumber[20];
	char Email[20];
};
//构建联系人信息结构体

struct ContactPerson Infor[100];//接收数据的地址

void Display()
{
	FILE *fp;
	int i;
	int count = 0;
	fp = fopen("data.txt","rb");
	while(!feof(fp))
	{
		if(fread(&Infor[count], LENGTH, 1, fp) ==1)
		    count++	;		
    }
    fclose(fp);
    printf("%-20s%-20s%-20s%-20s%-20s\n","姓名","职务","职称","电话","邮箱");
    for(i=0; i<count; i++)
	{
    	 printf("%-20s%-20s%-20s%-20s%-20s\n",Infor[i].Name,Infor[i].Occupation,Infor[i].Title,Infor[i].Phonenumber,Infor[i].Email);
	}
}

void Input()
{
	FILE *fp;
	int i;
	int p = 0;
	int count = 0;
	fp = fopen("data.txt","rb");
	
	while(!feof(fp))//判断文件是否结束
	{
		fread(&Infor[count], LENGTH, 1, fp);
		count++;
	}
	
	fclose(fp);//关闭文件
	
	fp = fopen("data.txt","wb");
	printf("输入您想录入的人数：");
	scanf("%d",&p);
	
	for(i=0; i<count+p; i++)//重新录入数据
	{
		if (Infor[i].Phonenumber!=0 && i<count)
		{
			fwrite(&Infor[i], LENGTH, 1, fp);
		}
		
		if (i>=count)
		{
			printf("请输入姓名：\n");
		    scanf("%s",&Infor[i].Name);
		    printf("请输入职位：\n");
		    scanf("%s",&Infor[i].Occupation);
		    printf("请输入职称：\n");
		    scanf("%s",&Infor[i].Title);
		    printf("请输入电话：\n");
		    scanf("%s",&Infor[i].Phonenumber);
		    printf("请输入邮箱：\n");
		    scanf("%s",&Infor[i].Email);
		    fwrite(&Infor[i], LENGTH, 1, fp);
		}

	}
	fclose(fp);
	return;
}
void Delete()
{
	FILE *fp;
	int i;
	int j;
	int count = 0;
	char ch[20];
	char Name[20];
	fp = fopen("data.txt","rb");
	while(!feof(fp))//判断文件是否结束
	{
		if(fread(&Infor[count], LENGTH, 1, fp)==1)
		count++;
	}
	fclose(fp);//关闭文件
	if(count == 0)//查看当前通讯录是否为空
	{
		printf("通讯录为空\n");
		return;
	}
	else
	{
		system("cls");//清屏
		Display();
	}
	fp = fopen("data.txt","ab");
	printf("请输入要删除联系人的姓名\n");
	scanf("%s",&Name);
	
	for(i=0; i<count; i++)
	{
	    if(strcmp(Name, Infor[i].Name) == 0)
	    {
		    printf("确定要删除此联系人吗？(yes or no)\n");
		    scanf("%s", ch);
		    if(strcmp(ch, "yes") == 0)
		    {
				for(j=i; j<count; j++)
				{
					Infor[j] = Infor[j+1];
				}
				count--;
				break;
			}
			else if(strcmp(ch, "no") == 0)
			{
				break;
			
			}
			else
			{
				i--;
				printf("输入错误\n");
			}
	    }
	}
    fclose(fp);
}
void Update()
{

	FILE *fp;
	int i;
	int j;
	
	int count = 0;
	char ch[20];
	char Name[20];
	fp = fopen("data.txt","rb");
	while(!feof(fp))
	{
		  
		if(fread(&Infor[count], LENGTH, 1, fp) ==1)
		    count++	;		
    }
    if(count == 0)//查看当前通讯录是否为空
	{
		printf("通讯录为空\n");
		return;
	}
	else
	{
		system("cls");//清屏
		Display();
	}
	fclose(fp);
	fp = fopen("data.txt","ab");	
	printf("请输入要修改的信息联系人的姓名:\n");
	scanf("%s",&Name);
	for(i=0; i<count; i++)
	{
	    if(strcmp(Name, Infor[i].Name) == 0)
	    {
	        printf("确定要修改此联系人的信息吗？(yes or no)\n");
		    scanf("%s", ch);
		    if(strcmp(ch, "yes") == 0)
	        {
		        for(j=0; j<count; j++)
	            {
		            if(strcmp(Name, Infor[j].Name) == 0)
		            {
			            printf("请输入修改后的姓名：\n");
		                scanf("%s",&Infor[j].Name);
		                printf("请输入修改后的职位：\n");
		                scanf("%s",&Infor[j].Occupation);
		                printf("请输入修改后的职称：\n");
		                scanf("%s",&Infor[j].Title);
		                printf("请输入修改后的电话：\n");
		                scanf("%s",&Infor[j].Phonenumber);
		                printf("请输入修改后的邮箱：\n");
		                scanf("%s",&Infor[j].Email);
		                printf("已更新成功\n");
		                break;
		           }
		       }
	        }
	        else if(strcmp(ch, "no") == 0)
	        {
	        	break;
			}
			else
			{
				i--;
				printf("输入错误\n");
			}
	    }
	    fclose(fp);
	}
}
void Search()
{
	FILE *fp;
	int i;
	int n;
	int count = 0;
	char Name[20];
    char Occupation[20];
    char Title[20];
    char Phonenumber[20];
    char Email[20];
	char ch;
	fp = fopen("data.txt","rb");
	while(!feof(fp))
	{
		  
		if(fread(&Infor[count], LENGTH, 1, fp) ==1)
		    count++	;		
    }
    fclose(fp);
    if(count == 0)
	{
		printf("通讯录为空\n");
		return;
	}
	printf("您想以什么方式查找联系人\n");
	printf("1.姓名\n");
	printf("2.职位\n");
	printf("3.职称\n");
	printf("4.电话\n");
	printf("5.邮箱\n");
	printf("请输入：");
	scanf("%s",&n);
	
	switch(n)
	{
		case '1':
			Display();
			printf("请输入要查找人的姓名:\n");
	        scanf("%s",&Name);
	        for(i=0; i<count; i++)
	        {
		        if(strcmp(Name, Infor[i].Name) == 0)
		        {
			        printf("%-20s%-20s%-20s%-20s%-20s\n","姓名","职务","职称","电话","邮箱");
	                printf("%-20s%-20s%-20s%-20s%-20s\n",Infor[i].Name,Infor[i].Occupation,Infor[i].Title,Infor[i].Phonenumber,Infor[i].Email);
		        }
		    }
            break;
        case '2':
        	Display();
		    printf("请输入要查找人的职位:\n");
	        scanf("%s",&Occupation);
	        for(i=0; i<count; i++)
	        {
		        if(strcmp(Occupation, Infor[i].Occupation) == 0)
		        {
			        printf("%-20s%-20s%-20s%-20s%-20s\n","姓名","职务","职称","电话","邮箱");
	                printf("%-20s%-20s%-20s%-20s%-20s\n",Infor[i].Name,Infor[i].Occupation,Infor[i].Title,Infor[i].Phonenumber,Infor[i].Email);
		        }
            }
			break;    
		case '3':
			Display();
		    printf("请输入要查找人的职称:\n");
	        scanf("%s",&Title);
	        for(i=0; i<count; i++)
	        {
		        if(strcmp(Title, Infor[i].Title) == 0)
		        {
			        printf("%-20s%-20s%-20s%-20s%-20s\n","姓名","职务","职称","电话","邮箱");
	                printf("%-20s%-20s%-20s%-20s%-20s\n",Infor[i].Name,Infor[i].Occupation,Infor[i].Title,Infor[i].Phonenumber,Infor[i].Email);
		        }
            }
            break;
		case '4':
			Display();
			printf("请输入要查找人的电话:\n");
	        scanf("%s",&Name);
	        for(i=0; i<count; i++)
	        {
		        if(strcmp(Phonenumber, Infor[i].Phonenumber) == 0)
		        {
			        printf("%-20s%-20s%-20s%-20s%-20s\n","姓名","职务","职称","电话","邮箱");
	                printf("%-20s%-20s%-20s%-20s%-20s\n",Infor[i].Name,Infor[i].Occupation,Infor[i].Title,Infor[i].Phonenumber,Infor[i].Email);
		        }
            }
            break;
        case '5':
        	Display();
        	printf("请输入要查找人的邮箱:\n");
	        scanf("%s",&Email);
	        for(i=0; i<count; i++)
	        {
		        if(strcmp(Email, Infor[i].Email) == 0)
		        {
			        printf("%-20s%-20s%-20s%-20s%-20s\n","姓名","职务","职称","电话","邮箱");
	                printf("%-20s%-20s%-20s%-20s%-20s\n",Infor[i].Name,Infor[i].Occupation,Infor[i].Title,Infor[i].Phonenumber,Infor[i].Email);
		        }
            }
            break;
		default:
			printf("输入错误\n");
				
	}	
}
void Sort()
{
    FILE *fp;
	struct ContactPerson temp;
	int i = 0;
	int j = 0;
	int count = 0;
	fp = fopen("data.txt","rb");
	while(!feof(fp))
	{
		if(fread(&Infor[count], LENGTH, 1, fp) ==1)
		    count++	;		
    }
    fclose(fp);
    fp = fopen("data.txt","rb");
	for(i=0; i<count-1; i++)
	{
		for(j=i+1; j<count; j++)
		{
			if(strcmp(Infor[i].Name, Infor[j].Name) > 0)
			{
				temp = Infor[i];
				Infor[i] = Infor[j];
				Infor[j] = temp;
			}
		}
		fwrite(&Infor[i], LENGTH, 1, fp);
	}
	Display();
	fclose(fp);
}
void Menu()
{
	printf("----------菜单----------\n");
	printf("-    1.添加联系人      -\n");
	printf("-    2.删除联系人      -\n");
	printf("-    3.修改联系人      -\n");
	printf("-    4.显示联系人      -\n");
	printf("-    5.查找联系人      -\n");
	printf("-    6.排序并输出      -\n");
	printf("-    7.退出通讯录      -\n");
	printf("------------------------\n");
	printf("请输入您的选择：");
}
int main()
{
	int n;
	while(1)
	{
		Menu();
		scanf("%s",&n);
		
		switch(n)
		{
			case '1':
				system("cls");
				Input();
				system("pause");
				system("cls");
				break;
			case '2':
				system("cls");
				Delete();
				system("pause");
				system("cls");
				break;
			case '3':
				system("cls");
				Update();
				system("pause");
				system("cls");
				break;
			case '4':
				system("cls");
				Display();
				system("pause");
				system("cls");
				break;
			case '5':
				system("cls");
				Search();
				system("pause");
				system("cls");
				break;
			case '6':
				system("cls");
			    Sort();
			    system("pause");
				system("cls");
			    break;
			case '7':
				return 0;
			default:
				printf("输入错误");
				break;
		}
	}
}

